#!/bin/bash

if [ `uname -m` != "x86_64" ]; then
	echo "不能在32位系统下安装服务端"
	exit
fi

if [ `whoami` != "root" ]; then
	echo '请在root用户运行安装脚本'
	echo '可尝试输入 sudo su root'
	exit
fi

ulimit -n 2048

server_start(){
	if [ "`netstat -anu|grep 8211`" != "" ]; then
		echo '服务端正在运行请先使用 sh pal.sh stop 命令停止服务端'
		exit
	fi
	su - pal -c 'screen -dmS pal ~/Steam/steamapps/common/PalServer/PalServer.sh -useperfthreads -NoAsyncLoadingThread -UseMultithreadForDS'
	
	echo '服务器已启动!'
	echo '服务器地址：'
	curl ifconfig.me
	echo ":8211"
}

server_config(){
	if [ "`netstat -anu|grep 8211`" != "" ]; then
		echo '服务端正在运行请先使用 sh pal.sh stop 命令停止服务端'
		exit
	fi
	su - pal -c '''
		cd ~/Steam/steamapps/common/PalServer
		cp -f DefaultPalWorldSettings.ini Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		
		read -p "请填写服务器进入密码（默认为无密码）：" replace_text
		if [ ! -z "$replace_text" ]; then
			sed -i "s/ServerPassword=\"\"/ServerPassword=\"$replace_text\"/g" Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		fi
		
		read -p "请填写管理密码（不输入不开启管理）：" replace_text
		if [ ! -z "$replace_text" ]; then
			sed -i "s/AdminPassword=\"\"/AdminPassword=\"$replace_text\"/g" Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		fi
		
		read -p "请填写服务器名称（默认为Default Palworld Server）：" replace_text
		if [ ! -z "$replace_text" ]; then
			sed -i "s/Default Palworld Server/$replace_text/g" Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		fi
		
		read -p "请填写服务器介绍（默认为空）：" replace_text
		if [ ! -z "$replace_text" ]; then
			sed -i "s/ServerDescription=\"\"/ServerDescription=\"$replace_text\"/g" Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		fi
		
		read -p "孵蛋时间设置（0-320 默认为72小时）：" replace_text
		if [ ! -z "$replace_text" ]; then
			sed -i "s/PalEggDefaultHatchingTime=72.000000/PalEggDefaultHatchingTime=$replace_text/g" Pal/Saved/Config/LinuxServer/PalWorldSettings.ini
		fi
	'''
	echo '已更改设置!'
	server_start
}

server_stop(){
	killall -u pal
	echo '已停止服务端!'
}

inst(){
	echo '开始安装服务端'
	useradd -m pal
	su - pal -c ' 
		mkdir ~/steamcmd
		cd ~/steamcmd
		curl -sqL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" | tar zxvf -	
		./steamcmd.sh +login anonymous +app_update 2394010 validate +quit
		mkdir ~/.steam
		mkdir ~/.steam/sdk64
		ln -s ~/steamcmd/linux64/steamclient.so ~/.steam/sdk64/steamclient.so
		
		cd ~/Steam/steamapps/common/PalServer/Pal
		mkdir Saved
		cd Saved
		mkdir Config
		cd Config
		mkdir LinuxServer
	'
	echo '安装完成!'
	server_config
}

server_update(){
	if [ "`netstat -anu|grep 8211`" != "" ]; then
		echo '服务端正在运行请先使用 sh pal.sh stop 命令停止服务端'
		exit
	fi
	
	su - pal -c ' 
		cd ~/steamcmd
		./steamcmd.sh +login anonymous +app_update 2394010 +quit
	'
	echo '更新完成!'
	server_start
}

debian(){
	dpkg --add-architecture i386
	apt-get update -y
	apt-get install lib32gcc-s1 -y
	apt-get install screen -y
	apt-get install psmisc -y
	inst
}

centos(){
	yum update -y
	yum install glibc.i686 libstdc++.i686 -y
	yum install screen -y
	yum install psmisc -y
	inst
}

if [ -z "$1" ]; then
	exit
fi

if [ $1 = 'update' ]; then
	server_update
elif [ $1 = 'stop' ]; then
	server_stop
elif [ $1 = 'start' ]; then
	server_start
elif [ $1 = 'config' ]; then
	server_config
elif [ $1 = 'inst' ]; then
	if [ -x "$(command -v apt-get)" ]; then
		debian
	elif [ -x "$(command -v yum)" ]; then
		centos
	else
		echo '很抱歉本系统不支持这个脚本'
	fi
else
	echo '请提供正确的参数'
fi

